/**
 * Created by hxpeng on 2017/4/18.
 */
(function () {
    'use strict';

    angular.module('App.pages.dynamic')
        .controller('DynamicInfoCtrl', DynamicInfoCtrl);

    function DynamicInfoCtrl($scope,DynamicModel,$stateParams,AppUtils,$timeout,LoginAccountAuth) {

        var vm = this;
        vm.dynamic = {};
        vm.comment = "";
        vm.dynamicId = "";
        vm.hadLikesStr = "点赞+1";
        vm.toReply = null;
        vm.loginAccount = {};

        vm.dynamicId = _.clone($stateParams.dynamicId);
        if(!AppUtils.isUndefinedOrNull(vm.dynamicId)){
            DynamicModel.getDynamicInfo({id:vm.dynamicId}).$promise.then(function(response){
                if(response.stat == 'success'){
                    vm.dynamic = _.clone(response.dynamicInfo);
                    if(vm.dynamic.hadLikes){
                        vm.hadLikesStr = "已经点过啦";
                    }
                    $scope.loadMoreComment();
                }
            })
        }

        $scope.$on('$ionicView.enter', function(){
            var dynamicCommentBox = $('#dynamic-comment-box');
            var $androidMask = dynamicCommentBox.find('.weui-mask');
            LoginAccountAuth.getAccount().then(function(data){
                vm.loginAccount = data;
            });

            $("body").on("click","#showCommentsBtn",function(){
                console.log(1111111);
                $(this).blur();
                dynamicCommentBox.fadeIn(200);
                $androidMask.on('click',function () {
                    $scope.$apply(function(){
                        vm.toReply = null;
                    });
                    dynamicCommentBox.fadeOut(200);
                });
                $timeout(function(){
                    dynamicCommentBox.find("textarea").focus();
                },200)
            })

            var $toast = $('#toast');
            $('#showToast').unbind("click").bind('click', function(){
                if(vm.dynamic.hadLikes == false){
                    DynamicModel.likeDynamic({id:vm.dynamic.id}).$promise.then(function(response){
                        if(response.stat == 'success'){
                            vm.dynamic.hadLikes = true;
                            setTimeout(function () {
                                $scope.$apply(function(){
                                    vm.hadLikesStr = "已经点过啦";
                                });
                            }, 1500);
                        }else {
                            AppUtils.showTipsTop("操作失败，请稍后再试");
                        }
                    });
                }
                if ($toast.css('display') != 'none') return;
                $toast.fadeIn(100);
                setTimeout(function () {
                    $toast.fadeOut(100);
                }, 1000);
            });
        });
        //评论
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 0;


        $scope.loadMoreComment = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.loadComment();
            }
        }

        vm.loadComment = function(){
            var params = {};
            params.dynamicId = vm.dynamic.id;
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            DynamicModel.loadComment(params).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                    vm.dynamic.comments = response.pageTotalItemSize;
                }
            }).finally(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }



        $scope.commitComment = function(){
            if(!AppUtils.isUndefinedOrNull(vm.comment)){
                var obj = {};
                obj.dynamicId = vm.dynamic.id;
                obj.comment = vm.comment;
                if(!AppUtils.isUndefinedOrNull(vm.toReply)){
                    obj.toMemberId = vm.toReply.id;
                    obj.toMemberName = vm.toReply.name;
                    obj.parentCommentId = vm.toReply.parentCommentId;
                }
                DynamicModel.commitComment(obj).$promise.then(function(response){
                    if(response.stat == 'success'){
                        AppUtils.showTipsTop("提交成功！");
                        $('#dynamic-comment-box').fadeOut(200);
                        vm.comment = "";
                        vm.exhortaions = [];
                        vm.pageIndex = 1;
                        vm.loadComment();
                    }else{
                        AppUtils.showTipsTop("提交评论失败了！");
                    }
                })
            }else{
                AppUtils.showTipsTop("请填写评论！");
            }
        }

        $scope.deleteComment = function(commentId,index){
            if(!AppUtils.isUndefinedOrNull(commentId)){
                DynamicModel.deletedComment({id:commentId}).$promise.then(function(response){
                    if(response.stat == 'success'){
                        setTimeout(function () {
                            $scope.$apply(function(){
                                vm.exhortaions.splice(index, 1);
                            });
                        }, 10);
                    } else {
                        AppUtils.showTipsTop("操作失败，请稍后再试");
                    }
                });
            }
        }


        $scope.replyComment = function(commentId,memberId,memberName,parentCommentId,$event){
            // var keepGoing = true;
            // if($event.target.nodeName === 'SPAN' && $event.target.classList.contains("ngTruncateToggleText")){
            //     keepGoing = false;
            // }
            // if($event.target.nodeName === 'A'){
            //     keepGoing = false;
            // }
            // if(keepGoing){
            //
            // }
            if(!AppUtils.isUndefinedOrNull(memberId) || !AppUtils.isUndefinedOrNull(memberName)){
                vm.toReply = {};
                vm.toReply.id = memberId;
                vm.toReply.name = memberName;
                if(!AppUtils.isUndefinedOrNull(parentCommentId)){
                    vm.toReply.parentCommentId = parentCommentId;
                }else{
                    vm.toReply.parentCommentId = commentId;
                }
                $("#showCommentsBtn").click();
            }
        }

        $scope.likeComment = function(item){
            if(item.hadLikes != '1'){
                DynamicModel.likeComment({id:item.id}).$promise.then(function(response){
                    if(response.stat === 'success'){
                        item.hadLikes = '1';
                        item.likes = Number(item.likes + 1);
                    } else {
                        AppUtils.showTipsTop("操作失败，请稍后再试");
                    }
                });
            }
        }

    }

})();
